package com.gome.oa.listener;

import java.util.Date;

import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;

import com.gome.oa.custom.CustomCheckbox;
import com.gome.oa.dialog.FillFormDialog;
import com.gome.oa.model.Day;

/**
 *
 * @author: tang li
 * @date: 2016年8月21日
 * @desc:
 *
 */
public class FormModelListener implements TableModelListener {
	
	private FillFormDialog fillFormDialog;
	
	public FormModelListener(FillFormDialog fillFormDialog) {
		super();
		this.fillFormDialog = fillFormDialog;
	}

	@Override
	public void tableChanged(TableModelEvent event) {
		int row = event.getFirstRow();
		int column = event.getColumn();
		TableModel model = (TableModel) event.getSource();
		if (row < model.getRowCount() - 1 && column == 3) {
			CustomCheckbox checkbox = (CustomCheckbox) model.getValueAt(row, 0);
			Day day = checkbox.getDay();
			Date modifiedOffDate = (Date) model.getValueAt(row, 3);
			day.setOffDate(modifiedOffDate);
			//re-calculate time
			fillFormDialog.calculateHour(day);
			model.setValueAt(day.getHour(), row, 5);
			fillFormDialog.updateHours();
		}
	}

}
